package com.qingcloud.fexecutor.base.enums;

/**
 * 数据库值类型ENUM
 *
 * @author wangjinnan
 */
public enum DataBaseValueTypeEnum {

    /**
     * 数据库字段的数据类型
     */
    VARCHAR(1, "VARCHAR", true, "32"),
    CHAR(2, "CHAR", true, "32"),
    DECIMAL(3, "DECIMAL", true, "10,2"),
    UNSIGNED_DECIMAL(4, "UNSIGNED DECIMAL", true, "10,2"),
    FLOAT(5, "FLOAT", true, "10,2"),
    UNSIGNED_FLOAT(6, "UNSIGNED FLOAT", true, "10,2"),
    DOUBLE(7, "DOUBLE", true, "10,2"),
    UNSIGNED_DOUBLE(8, "UNSIGNED DOUBLE", true, "10,2"),
    TINYINT(9, "TINYINT", true, "4"),
    UNSIGNED_TINYINT(10, "UNSIGNED TINYINT", true, "3"),
    SMALLINT(11, "SMALLINT", true, "6"),
    UNSIGNED_SMALLINT(12, "UNSIGNED SMALLINT", true, "5"),
    MEDIUMINT(13, "MEDIUMINT", true, "9"),
    UNSIGNED_MEDIUMINT(14, "UNSIGNED MEDIUMINT", true, "8"),
    INT(15, "INT", true, "11"),
    UNSIGNED_INT(16, "UNSIGNED INT", true, "10"),
    BIGINT(17, "BIGINT", true, "20"),
    UNSIGNED_BIGINT(18, "UNSIGNED BIGINT", true, "20"),
    DATE(19, "DATE", false, ""),
    DATETIME(20, "DATETIME", false, ""),
    BLOB(21, "BLOB", false, ""),
    TEXT(22, "TEXT", false, "");

    /**
     * 类型编码
     */
    public final Integer code;

    /**
     * MYSQL数据库字段数据类型名称
     */
    public final String mysqlName;

    /**
     * 是否能配置长度
     */
    public final Boolean canLen;

    /**
     * 是否能配置长度
     */
    public final String defaultLen;

    DataBaseValueTypeEnum(int code, String mysqlName, boolean canLen, String defaultLen) {
        this.code = code;
        this.mysqlName = mysqlName;
        this.canLen = canLen;
        this.defaultLen = defaultLen;
    }

    /**
     * 根据mysql的类型名称，获取mysql的编码
     *
     * @param name 类型名称
     * @return 类型编码
     */
    public static Integer getMySqlCode(String name) {
        DataBaseValueTypeEnum[] uss = values();
        for (DataBaseValueTypeEnum us : uss) {
            if (us.mysqlName.equalsIgnoreCase(name)) {
                return us.code;
            }
        }
        return 1;
    }

    public static DataBaseValueTypeEnum getDataBaseValueTypeEnumBy(String typeName) {
        String[] typeNameArr = typeName.split(" ");
        String mysqlName;
        if(typeNameArr.length==2){
            mysqlName = typeNameArr[1]+" "+typeNameArr[0];
        }else{
            mysqlName = typeName;
        }
        DataBaseValueTypeEnum[] uss = values();
        for (DataBaseValueTypeEnum us : uss) {
            if (us.mysqlName.equals(mysqlName)) {
                return us;
            }
        }
        return null;
    }

    /**
     * 根据编码获取mysql的类型名称
     *
     * @param code 类型编码
     * @return 类型名称
     */
    public static String getMySqlType(Integer code) {
        DataBaseValueTypeEnum[] uss = values();
        for (DataBaseValueTypeEnum us : uss) {
            if (us.code.equals(code)) {
                return us.mysqlName;
            }
        }
        return VARCHAR.mysqlName;
    }

    /**
     * 根据编码获取是否能配置长度
     *
     * @param code 类型编码
     * @return 类型名称
     */
    public static DataBaseValueTypeEnum getByCode(Integer code) {
        DataBaseValueTypeEnum[] uss = values();
        for (DataBaseValueTypeEnum us : uss) {
            if (us.code.equals(code)) {
                return us;
            }
        }
        return VARCHAR;
    }
}
